package com.localization;

import java.util.Arrays;

/**
 * Created by Edward on 2017/1/7.
 */
public class HostCmd {

    private byte[] mHostCmd  = new byte[1024];
    private int size = 1024;
    private int postion = 0;
    private int length  = 0;

    public HostCmd()
    {
        length = 0;
        postion = 0;
        size = 1024;
     //   mHostCmd = Arrays.copyOf(cmd,len);
     //   postion = 0;
     //   length = len;
    }

    public void AddData(byte[] cmds,int len)
    {
        if(len <= size) {
            System.arraycopy(cmds, 0, mHostCmd, 0, len);
            postion = 0;
            length = len;
        }
    }

    public void clear()
    {
        length = 0;
        postion = 0;
        size = 1024;
    }

    public byte[] getmHostCmd()
    {
        return mHostCmd;
    }
    public  int getLength()
    {
        return length;
    }

    public byte[] getStringVal(int len)
    {
        byte[] data = new byte[len];
        if((postion +len) <= length) {
           for(int cnt = 0;cnt < len;cnt++)
           {
               data[cnt] = mHostCmd[postion++];
           }
            return data;
        }else{
            return null;
        }
    }

    public int get8BitVal()
    {
        if((postion +1) <= length) {
            return (mHostCmd[postion++]&0xff);
        }else {
            return -1;
        }
    }

    public int get16BitVal()
    {
        int data = 0;
        if((postion + 2) <= length)
        {
            data = mHostCmd[postion++]&0xff;
            data <<=8;
            data |= mHostCmd[postion++]&0xff;
            return data;
        }else{
            return -1;
        }
    }

    public int get32BitVal()
    {
        int data = 0;
        if((postion + 4) <= length)
        {
            data = mHostCmd[postion++]&0xff;
            data <<=8;
            data |= mHostCmd[postion++]&0xff;
            data <<=8;
            data |= mHostCmd[postion++]&0xff;
            data <<=8;
            data |= mHostCmd[postion++]&0xff;
            return data;
        }else{
            return -1;
        }
    }
}
